/* Copyright 2011 Thomas R. Mackel (tmackel@gmail.com) */

package com.civtechtracker;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;

public class Tech {
	private static Integer numTechs = 0;
	public Integer id;
	public String name;
	public Integer basecost;
	public Integer displaycost;
	public String desc;
	public List<Credit> credits;
	public Boolean owned;
	public Boolean inCart;
	public Integer creditsReceived;
	public String prereq;
	public Integer primaryColor;
	public Integer secondaryColor;

	public enum TechColors {
		ORANGE,
		GREEN,
		BLUE,
		RED,
		YELLOW,
		ENGINEERING,
		MATHEMATICS,
		LITERACY,
		MYSTICISM
	}
	
	public static void resetIdCounter() {
		numTechs = 0;
	}
	
	public Tech Clone(){
		Tech t = new Tech(name, basecost, TechColors.ORANGE, credits);
		numTechs--;
		t.id = id;
		t.name = name;
		t.basecost = basecost;
		t.displaycost = displaycost;
		t.desc = desc;
		t.owned = owned;
		t.inCart = false;
		t.creditsReceived = creditsReceived;
		t.prereq = prereq;
		t.primaryColor = primaryColor;
		t.secondaryColor = secondaryColor;
		
		return t;
	}
	
	public Tech(String n, Integer c, TechColors tc) {
		id = numTechs++;
		name = n;
		basecost = c;
		displaycost = c;
		desc = "";
		credits = new ArrayList<Credit>();
		owned = false;
		inCart = false;
		creditsReceived = 0;
		prereq = null;
		setColors(tc);
	}

	public Tech(String n, Integer c, TechColors tc, List<Credit> cr) {
		this(n, c, tc);
		if (null != cr) {
			for (Credit cred : cr) {
				credits.add(new Credit(cred.creditTowards, cred.amount));
			}
			this.removeCredit(n);
		}
	}

	public Tech(String n, Integer c, TechColors tc, List<Credit> cr, String prereqTech) {
		this(n, c, tc, cr);
		prereq = prereqTech;
		this.removeCredit(prereq);
	}
	
	public Tech(String n, Integer c, TechColors tc, List<Credit> cr, String prereqTech, String description){
		this(n, c, tc, cr, prereqTech);
		desc = description;
	}

	public void addOrUpdateCredit(String n, Integer a) {
		Boolean success = false;
		for (Credit c : credits) {
			if (c.creditTowards.equals(n)) {
				c.amount = a;
				success = true;
			}
		}
		if (!success) {
			credits.add(new Credit(n, a));
		}
		this.removeCredit(this.name);
	}
	
	public void removeCredit(String n) {
		Credit removeTarget = null;
		for (Credit c : credits){
			if (c.creditTowards.equals(n)){
				removeTarget = c;
			}				
		}
		if (null != removeTarget){
			credits.remove(removeTarget);
		}
	}
	
	private void setColors( TechColors tc ){
		switch (tc){
		case ORANGE:
			primaryColor = Color.parseColor("#ffffa500");
			secondaryColor = primaryColor;
			break;
		case GREEN:
			primaryColor = Color.parseColor("#ff32cd32");
			secondaryColor = primaryColor;
			break;
		case BLUE:
			primaryColor = Color.parseColor("#ff1e90ff");
			secondaryColor = primaryColor;
			break;
		case RED:
			primaryColor = Color.parseColor("#ffff6347");
			secondaryColor = primaryColor;
			break;
		case YELLOW:
			primaryColor = Color.parseColor("#ffffff00");
			secondaryColor = primaryColor;
			break;
		case ENGINEERING:
			primaryColor = Color.parseColor("#ffffa500");
			secondaryColor = Color.parseColor("#ff32cd32");
			break;
		case MATHEMATICS:
			secondaryColor = Color.parseColor("#ff1e90ff");
			primaryColor = Color.parseColor("#ff32cd32");
			break;
		case LITERACY:
			primaryColor = Color.parseColor("#ff1e90ff");
			secondaryColor = Color.parseColor("#ffff6347");
			break;
		case MYSTICISM:
			primaryColor = Color.parseColor("#1e90ff");
			secondaryColor = Color.parseColor("#ffff00");
			break;
		default:
			primaryColor = Color.BLACK;
			secondaryColor = Color.BLACK;
			break;
		}
	}
}
